<?php

namespace plugin\thinkphp\form;

use ExAdmin\ui\contract\UploaderAbstract;
use ExAdmin\ui\response\Response;
use Intervention\Image\ImageManagerStatic as Image;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use think\facade\Filesystem;
use think\facade\Log;

class Uploader extends UploaderAbstract
{


    /**
     * 写入文件
     * @param string $filename 文件名
     * @param string $content 文件内容
     * @return bool
     */
    protected function put(string $filename, $content): bool
    {
        return app('filesystem')->disk($this->disk)->put($filename, $content);
    }

    /**
     * 是否存在
     * @param string $path 路径
     * @return bool
     */
    public function exists(string $path): bool
    {

        return app('filesystem')->disk($this->disk)->has($path);
    }

    /**
     * 返回访问url
     * @param string $path 路径
     * @return string
     */
    public function url(string $path): string
    {

        return app('filesystem')->disk($this->disk)->url($path);
    }

    /**
     * 临时目录
     * @return string
     */
    protected function tempDirectory(): string
    {
        return runtime_path('tmp');
    }

    /**
     * 写入文件
     * @param $content 文件内容
     * @param $filename 文件名
     * @return bool|string
     */
    public function putContent($content, $filename)
    {
        $path = $this->directory . $filename . '.' . $this->extension;
        return app('filesystem')->disk($this->disk)->put($path, $content);
    }

    /**
     * 上传
     * @param \Closure $complete
     * @param bool $exists 判断秒传
     * @return Response
     */
    public function upload(\Closure $complete = null, bool $exists = false)
    {
        return parent::upload(function (UploadedFile $file) {
            if ($this->form) {
                $component = $this->form->getImageComponent();
                if ($component) {
                    $thumbnail = $component->getThumbnail();
                    //图片处理
                    $interventionCall = $component->getInterventionCall();
                    if (count($interventionCall) > 0) {
                        $image = Image::make($file->getRealPath());
                        foreach ($interventionCall as $call) {
                            call_user_func_array([$image, $call['method']], $call['arguments']);
                        }
                        $file = $image->encode(null, null)->getEncoded();
                    }
                    //生成缩略图
                    if (count($thumbnail) > 0) {
                        if ($file instanceof UploadedFile) {
                            $data = $file->getRealPath();
                            $filename = request()->param('identifier');
                        } else {
                            $data = $file;
                            $filename = md5($data);
                        }
                        foreach ($thumbnail as $name => $size) {
                            $image = Image::make($data);
                            list($width, $height) = $size;
                            $content = $image->resize($width, $height)
                                ->encode(null, null)
                                ->getEncoded();
                            $this->putContent($content, $filename . '-' . $name);
                        }
                    }

                }
            }
            return $file;
        }, request()->param('type') == 'file'); // TODO: Change the autogenerated stub
    }

    protected function path(string $path): string
    {
        return app('filesystem')->disk($this->disk)->path($path);
    }

    protected function putStream(string $filename, $resource): bool
    {
        return app('filesystem')->disk($this->disk)->putStream($filename, $resource);
    }
}
